﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using BbsbWebService.Enums;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.IO;
using System.Reflection;

namespace BbsbWebService.Classes
{
    [DataContract]
    public class StateItem : BaseItem
    {
        public StateItem()
            : base()
        {
        }

        private PowerState _state = PowerState.Unknown;

        [DataMember(EmitDefaultValue=false)]
        [DefaultValue(PowerState.Unknown)]
        public PowerState State 
        {
            get { return _state; }
            set
            {
                if (_state != value)
                {
                    _state = value;
                    NotifyPropertyChanged("State");
                }
            }
        }

        //public BitmapImage Image
        //{
        //    get
        //    {
        //        string resourcePath = "img/unknown.png";
        //        if (State == PowerState.On)
        //            resourcePath = "tick.png";
        //        else if (State == PowerState.Off)
        //            resourcePath = "cross.png";

        //        var image = new BitmapImage();
        //        string moduleName = this.GetType().Assembly.GetName().Name;
        //        string resourceLocation =
        //            string.Format("pack://application:,,,/{0};component/{1}", moduleName,
        //                          resourcePath);
        //        try
        //        {
        //            image.BeginInit();
        //            image.CacheOption = BitmapCacheOption.OnLoad;
        //            image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        //            image.UriSource = new Uri(resourceLocation, UriKind.RelativeOrAbsolute);
        //            image.EndInit();
        //        }
        //        catch (Exception ex)
        //        {
        //            Logging.Logger.Instance.LogException(ex, "Error creating image");
        //        }

        //        return image;
        //    }
        //}

        //public Image Image
        //{
        //    get
        //    {
        //        Image image = new Image();
        //        string resourcePath = "BbsbWebService.img.unknown.png";
        //        if (State == PowerState.On)
        //            resourcePath = "tick.png";
        //        else if (State == PowerState.Off)
        //            resourcePath = "cross.png";

        //        try
        //        {
        //            Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourcePath);
        //            if (fileStream != null)
        //            {
        //                PngBitmapDecoder bitmapDecoder = new PngBitmapDecoder(fileStream,
        //                    BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        //                ImageSource imageSource = bitmapDecoder.Frames[0];
        //                image.Source = imageSource;
        //            }
        //        }
        //        catch (Exception e)
        //        {
        //            Logging.Logger.Instance.LogMessage("Error creating image. ERROR: {0}", e.Message);
        //        }

        //        return image;
        //    }
        //}
    }
}
